const { think } = require('thinkjs');
const Base = require('./base.js');

module.exports = class extends Base {
  async listAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const query = this.get();

    if (query.name) {
      query.name = ['like', `%${query.name}%`];
    }
    if (query.phone) {
      query.phone = ['like', `%${query.phone}%`];
    }

    delete query.page;
    delete query.size;

    const data = await this.model('custom_list')
      .where(query)
      .order(['id DESC'])
      .page(page, size)
      .countSelect();

    return this.success(data);
  }

  async putAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('custom_list');

    if (id > 0) {
      await model.where({ id: id }).update(values);
      this.model('addLog').add({
        user_id: this.getLoginUserId(),
        title: '编辑客户资料',
        content: id,
      });
    } else {
      delete values.id;
      values.type = 1;
      const hasProduct = await model.where({ name: values.name }).find();
      if (!think.isEmpty(hasProduct)) {
        return this.fail(400, '名称已存在');
      }
      values.create_time = ['exp', 'CURRENT_TIMESTAMP()'];

      const addId = await model.add(values);
      this.model('addLog').add({
        user_id: this.getLoginUserId(),
        title: '新增客户资料',
        content: addId,
      });
    }
    return this.success(values);
  }

  async delAction() {
    const id = this.get('id');
    await this.model('custom_list')
      .where({ id: id })
      .limit(1)
      .delete();

    return this.success();
  }

  async listAllAction() {
    const data = await this.model('custom_list')
      .field('id, name')
      .select();
    return this.success(data);
  }

  async getSysDataAction() {
    const contractData = await this.model('contract_list')
      .field('custom_info, custom, id')
      .select();
    await this.model('custom_list')
      .where({ type: 2 })
      .delete();
    for (const item of contractData) {
      if (item.custom_info) {
        const arr = JSON.parse(item.custom_info);
        for (const itemChild of arr) {
          const has = await this.model('custom_list')
            .where({ name: itemChild.name })
            .find();
          if (
            think.isEmpty(has) ||
            (!think.isEmpty(has) && !has.phone && itemChild.phone) ||
            (!think.isEmpty(has) && !has.manage && itemChild.manage)
          ) {
            await this.model('custom_list').add({
              name: itemChild.name,
              phone: itemChild.phone,
              manage: itemChild.manage,
              type: 2,
              contract_id: item.id,
              status: item.status > 1 ? 2 : 1,
              contract_type: item.type,
            });
          }
        }
      } else if (item.custom) {
        const has = await this.model('custom_list')
          .where({ name: item.custom })
          .find();
        if (
          think.isEmpty(has) ||
          (!think.isEmpty(has) && !has.phone && item.custom_phone) ||
          (!think.isEmpty(has) && !has.manage && item.custom_name)
        ) {
          await this.model('custom_list').add({
            name: item.custom,
            phone: item.custom_phone,
            manage: item.custom_name,
            type: 2,
            contract_id: item.id,
            status: item.status > 1 ? 2 : 1,
            contract_type: item.type,
          });
        }
      }
    }
    return this.success();
  }
};
